package xyz.playedu.api.controller.backend;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.MapUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import xyz.playedu.api.utils.DateUtils;
import xyz.playedu.common.types.JsonResponse;
import xyz.playedu.common.types.paginate.DeviceNamePaginateFiler;
import xyz.playedu.common.types.paginate.PaginationResult;
import xyz.playedu.common.types.paginate.ProjectPaginateFiler;
import xyz.playedu.inspection.domain.DeviceNameDict;
import xyz.playedu.inspection.domain.ProjectDict;
import xyz.playedu.inspection.service.DeviceNameDictService;
import xyz.playedu.inspection.service.DeviceTypeDictService;
import xyz.playedu.inspection.service.ProjectDictService;

import java.util.HashMap;

@RestController
@Slf4j
@RequestMapping("/backend/v1/project")
public class ProjectDictController {
    @Autowired
    private ProjectDictService projectDictService;


    @PostMapping("/create")
    public JsonResponse create(@RequestBody ProjectDict projectDict) {
        HashMap<String, Object> data = new HashMap<>();
        projectDict.setCreateTime(DateUtils.getNowDate());
        data.put("data", projectDictService.save(projectDict));
        return JsonResponse.data(data);
    }

    @GetMapping("/getList")
    public JsonResponse getList(@RequestParam HashMap<String, Object> params){
        Integer page = MapUtils.getInteger(params, "page", 1);
        Integer size = MapUtils.getInteger(params, "size", 10);

        String project = MapUtils.getString(params, "project");
        ProjectPaginateFiler filer = new ProjectPaginateFiler();
        filer.setProject(project);

        PaginationResult<ProjectDict> result = projectDictService.paginate(page, size, filer);

        HashMap<String, Object> data = new HashMap<>();
        data.put("data",result.getData());
        data.put("total",result.getTotal());

        return JsonResponse.data(data);
    }
    @GetMapping("/getDeviceTypeById/{id}")
    public JsonResponse getDeviceTypeById(@PathVariable(name="id") Long id){
        HashMap<String, Object> map = new HashMap<>();
        map.put("data",projectDictService.getDeviceTypeById(id));
        return JsonResponse.data(map);
    }

    @PostMapping("/updateDeviceTypeById")
    public JsonResponse updateDeviceTypeById(@RequestBody ProjectDict projectDict) {
        HashMap<String, Object> data = new HashMap<>();
        projectDict.setUpdateTime(DateUtils.getNowDate());
        data.put("data", projectDictService.updateById(projectDict));
        return JsonResponse.data(data);
    }

    @GetMapping("/delInfoById/{id}")
    public JsonResponse delInfoById(@PathVariable(name="id") Long id){
        HashMap<String, Object> map = new HashMap<>();
        map.put("data",projectDictService.delInfoById(id));
        return JsonResponse.data(map);
    }

}
